package com.ttw.demo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.blankj.utilcode.util.UtilsTransActivity;
import com.blankj.utilcode.util.UtilsTransActivity4MainProcess;
import com.ttw.ActivityUtils;
import com.ttw.IntentJobService;
import com.ttw.IntentUtils;
import com.ttw.TTW;
import com.ttw.TTWLocal;
import com.ttw.ttw.R;


@SuppressLint("WrongConstant")
public class ScreenLockActivity extends Activity {

    @SuppressLint("ObsoleteSdkInt")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
//        if (Build.VERSION.SDK_INT > 18) {
//            lp.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
//        }
        window.setAttributes(lp);
        window.getDecorView().setSystemUiVisibility(0x0);
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        setContentView(R.layout.activity_back);
        TextView button = findViewById(R.id.bt_message);

        button.setText("TTW 存活中。。。");
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    public void onClick(View view) {
        finish();
    }


    public static void senNotification(Context context) {
        Intent intent = new Intent(context, ScreenLockActivity2.class);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            try {
                IntentUtils.startActivitySafe(context, intent, false);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
            return;
        }
        intent.addFlags(IntentUtils.FLAG_AUTH);
        NotificationCompat.Builder intentNotificationBuilder = TTWLocal.getConfiguration().getIntentNotificationBuilder(context);
        intentNotificationBuilder.setFullScreenIntent(PendingIntent.getActivity(context, 0, intent, 134217728), true);
//        intentNotificationBuilder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 134217728));
        NotificationManagerCompat from = NotificationManagerCompat.from(context);
        try {
            from.cancel(99);
            IntentJobService.scheduleService(context, intent, true);
            IntentUtils.startActivitySafe(context, intent);
            from.notify(99, intentNotificationBuilder.build());
            new Handler(Looper.getMainLooper()).postDelayed(new ActivityUtils.CancelNotificationRunnable(from), 2000);
        } catch (Exception e2) {
            e2.printStackTrace();
        }

    }

    public static void startActivity(Context application) {

        senNotification(application);

//        Intent intent = new Intent(application, ScreenLockActivity2.class);
//        ActivityUtils.hookJumpActivity(application, intent);

//        Intent intent = new Intent("com.ttw.demo.MY_BROCASTRECEIVER");
////第一个参数为广播接收器所在应用的包名
////        https://www.jianshu.com/p/85e85bcbee58?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io
////第二个参数为广播接收器包名+类名
//        intent.setComponent(new ComponentName("com.ttw.demo","com.ttw.demo.MyReceiver"));
//        Bundle bundle=new Bundle();
//        bundle.putString("key","Hello Receiver！");
//        intent.putExtras(bundle);
//        application.sendBroadcast(intent);

//        UtilsTransActivity4MainProcess.start(new UtilsTransActivity.TransActivityDelegate() {
//            @Override
//            public void onCreateBefore(@NonNull UtilsTransActivity activity, @Nullable Bundle savedInstanceState) {
//                super.onCreateBefore(activity, savedInstanceState);
//            }
//
//            @Override
//            public void onCreated(@NonNull UtilsTransActivity activity, @Nullable Bundle savedInstanceState) {
//                super.onCreated(activity, savedInstanceState);
//                activity.setContentView(R.layout.activity_dialog);
////                activity.findViewById<TextView>(R.id.utilActionLoadingMsgTv).text = "Trans Activity is showing..."
//            }
//
//            @Override
//            public void onStarted(@NonNull UtilsTransActivity activity) {
//                super.onStarted(activity);
//            }
//
//            @Override
//            public void onDestroy(@NonNull UtilsTransActivity activity) {
//                super.onDestroy(activity);
//            }
//
//            @Override
//            public void onResumed(@NonNull UtilsTransActivity activity) {
//                super.onResumed(activity);
//            }
//
//            @Override
//            public void onPaused(@NonNull UtilsTransActivity activity) {
//                super.onPaused(activity);
//            }
//
//            @Override
//            public void onStopped(@NonNull UtilsTransActivity activity) {
//                super.onStopped(activity);
//            }
//
//            @Override
//            public void onSaveInstanceState(@NonNull UtilsTransActivity activity, Bundle outState) {
//                super.onSaveInstanceState(activity, outState);
//            }
//
//            @Override
//            public void onRequestPermissionsResult(@NonNull UtilsTransActivity activity, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//                super.onRequestPermissionsResult(activity, requestCode, permissions, grantResults);
//            }
//
//            @Override
//            public void onActivityResult(@NonNull UtilsTransActivity activity, int requestCode, int resultCode, Intent data) {
//                super.onActivityResult(activity, requestCode, resultCode, data);
//            }
//
//            @Override
//            public boolean dispatchTouchEvent(@NonNull UtilsTransActivity activity, MotionEvent ev) {
//                return super.dispatchTouchEvent(activity, ev);
//            }
//        });


//        UtilsTransActivity.start(this, object : UtilsTransActivity.TransActivityDelegate() {
//            override fun onCreated(activity: UtilsTransActivity, savedInstanceState: Bundle?) {
//                super.onCreated(activity, savedInstanceState)
//                activity.setContentView(R.layout.common_dialog_loading)
//                activity.findViewById<TextView>(R.id.utilActionLoadingMsgTv).text = "Trans Activity is showing..."
//            }
//        })


        // 设置 Toast 布局（全局生效）
//        Toaster.setView(R.layout.toast_custom_view);
//        Toaster.setGravity(Gravity.CENTER);
//        Toaster.show(R.string.living);

//        Toast.makeText(application, "时间"+System.currentTimeMillis(), Toast.LENGTH_SHORT).show();

//        Intent intent = new Intent(application, ScreenLockActivity.class);
//        TTW.startActivity(application, intent);
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}